package org.water.common.wrapper;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

import org.apache.commons.lang3.StringEscapeUtils;

/**
 * xss处理
 * @author qzy
 *
 */
public class XSSRequestWrapper extends HttpServletRequestWrapper {

	/**
	 * 构造方法
	 * 
	 * @param request
	 */
	public XSSRequestWrapper(HttpServletRequest request) {
		super(request);
	}

	/**
	 * 处理参数值
	 */
	@Override
	public String[] getParameterValues(String parameter) {
		String[] values = super.getParameterValues(parameter);
		if (values == null) {
			return null;
		}
		int count = values.length;
		String[] encodedValues = new String[count];
		for (int i = 0; i < count; i++) {
			encodedValues[i] = dealString(values[i]);
		}
		return encodedValues;
	}

	/**
	 * 参数处理
	 */
	@Override
	public String getParameter(String parameter) {
		String value = super.getParameter(parameter);
		return dealString(value);
	}

	/**
	 * header处理
	 */
	@Override
	public String getHeader(String name) {
		String value = super.getHeader(name);
		return dealString(value);
	}

	/**
	 * 字符串处理   采用StringEscapeUtils工具类 实现
	 * @param value
	 * @return
	 */
	private String dealString(String value) {
		if (value != null) {
			StringEscapeUtils.escapeHtml4(value);
			StringEscapeUtils.escapeEcmaScript(value);
		}
		return value;
	}
}
